package com.syher.quartz.core.config.pool.monitor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by shenyuhang on 2018/2/25.
 */
public class ThreadPoolMonitor implements IThreadPoolMonitor {

    private static Logger logger = LoggerFactory.getLogger(ThreadPoolMonitor.class);

    ThreadPoolExecutor executor;
    long period;

    @Override
    public void run() {
        try {
            while (true) {
                monitorThreadPool();
                Thread.sleep(period * 1000);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    public void monitorThreadPool() {
        StringBuffer strBuff = new StringBuffer();
        strBuff.append("CurrentPoolSize : ").append(executor.getPoolSize());
        strBuff.append(" - CorePoolSize : ").append(executor.getCorePoolSize());
        strBuff.append(" - MaximumPoolSize : ").append(executor.getMaximumPoolSize());
        strBuff.append(" - ActiveTaskCount : ").append(executor.getActiveCount());
        strBuff.append(" - CompletedTaskCount : ").append(executor.getCompletedTaskCount());
        strBuff.append(" - TotalTaskCount : ").append(executor.getTaskCount());
        strBuff.append(" - isTerminated : ").append(executor.isTerminated());

        logger.debug(strBuff.toString());
    }

    public void setExecutor(ThreadPoolExecutor executor) {
        this.executor = executor;
    }

    public void setPeriod(long period) {
        this.period = period;
    }
}
